<?php


namespace App\Application\Controller\App;


use App\Application\Controller\BaseApplicationController;
use App\Application\Requests\App\Project\ProjectStoreRequest;
use App\Application\Requests\App\Project\ProjectUpdateRequest;
use App\Application\Services\App\ProjectService;

class ProjectController extends BaseApplicationController {
    public $noAuthActionList = ['getAuthLoginPageUrl'];
    /**
     * 获取应用下的项目分页
     *
     * @param ProjectService $service
     *
     * @return mixed
     */
    public function index(ProjectService $service) {
        return $this->success($service->projectPage());
    }
    
    /**
     * 新建项目
     *
     * @param ProjectStoreRequest $request
     * @param ProjectService      $service
     *
     * @return mixed
     * @throws \Exception
     */
    public function store(ProjectStoreRequest $request, ProjectService $service) {
        return $this->success($service->projectStore(
            $request->input(),
            $request->getRuleKeys()
        ));
    }
    
    /**
     * 获取应用授权页面url
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function getAuthLoginPageUrl(ProjectService $service, $id) {
        return $this->success($service->getAuthLoginPageUrl($id));
    }
    
    /**
     * 上传小程序代码
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function codeCommit(ProjectService $service, $id) {
        return $this->success($service->codeCommit($id));
    }
    
    /**
     * 提交审核
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function miniProgramSubmitAudit(ProjectService $service, $id) {
        return $this->success($service->miniProgramSubmitAudit($id));
    }
    
    /**
     * 发布已通过审核的小程序
     *
     * @param ProjectService $service
     * @param int            $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function miniProgramRelease(ProjectService $service, int $id) {
        return $this->success($service->miniProgramRelease($id));
    }
    
    /**
     * 更新项目
     * @param ProjectService       $service
     * @param ProjectUpdateRequest $request
     * @param                      $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function update(ProjectService $service, ProjectUpdateRequest $request, $id) {
        return $this->success($service->projectUpdate($id, $request->input(), $request->getRuleKeys()));
    }
    
    /**
     * 项目详情
     * @param ProjectService       $service
     * @param ProjectUpdateRequest $request
     * @param                      $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function show(ProjectService $service, $id) {
        return $this->success($service->getProjectItemElById($id, [], false));
    }
    
    /**
     * 发布代码并直接提交审核
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function publishMiniProgram(ProjectService $service, $id) {
        return $this->success($service->publishMiniProgram($id));
    }
    
    /**
     * 获取体验版二维码
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return \EasyWeChat\Kernel\Http\Response|mixed
     * @throws \Exception
     */
    public function getExperienceQrCode(ProjectService $service, $id){
        return $service->getExperienceQrCode($id);
    }
}